<template>
  <div style="padding: 20px">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="报工单编码" prop="code">
        <el-input
          v-model.trim="queryParams.code"
          style="width: 200px"
          placeholder="请输入报工单编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报工日期" prop="reportDate">
        <el-date-picker
          style="width: 200px"
          v-model.trim="queryParams.reportDate"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="请选择日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row> -->
    <div>
      <el-table v-loading="loading" border :data="dataList">
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="100"
          align="center"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="report_status" :value="row.status" />
            <!--ems_production_audit-->
          </template>
        </el-table-column>
        <el-table-column
          label="报工单编码"
          min-width="200"
          align="center"
          prop="code"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleView(row)" text>
              {{ row.code }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="报工日期"
          width="120"
          align="center"
          prop="reportDate"
          v-if="columns[2].visible"
        >
          <template v-slot="{ row }">
            {{ row.reportDate ? row.reportDate.split("T")[0] : "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="上报人"
          min-width="120"
          align="center"
          prop="createBy"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="机台"
          align="center"
          min-width="150"
          prop="machineName"
          v-if="columns[4].visible"
        ></el-table-column>
        <!-- <el-table-column
          label="机长"
          min-width="100"
          align="center"
          
          prop="headName"
          v-if="columns[5].visible"
        ></el-table-column> -->
        <el-table-column
          label="当班日期"
          min-width="100"
          align="center"
          prop="dutyDate"
          v-if="columns[6].visible"
        >
          <template v-slot="{ row }">
            {{ row.dutyDate ? row.dutyDate.split("T")[0] : "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          min-width="120"
          align="center"
          prop="remark"
          v-if="columns[7].visible"
        ></el-table-column>
        <el-table-column
          label="创建人"
          min-width="120"
          align="center"
          prop="createBy"
          v-if="columns[8].visible"
        ></el-table-column>
        <el-table-column
          label="创建时间"
          min-width="120"
          align="center"
          prop="createTime"
          v-if="columns[9].visible"
        >
          <template v-slot="{ row }">
            {{ row.createTime ? row.createTime.split("T")[0] : "" }}
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="操作"
          align="center"
          width="120"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tooltip
              content="修改"
              placement="top"
              v-if="scope.row.status != 1"
            >
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              content="删除"
              placement="top"
              v-if="scope.row.status != 1"
            >
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-button
              link
              type="primary"
              v-if="scope.row.status != 1"
              v-hasPermi="['production:printReport:audit']"
              @click="handleSubmit(scope.row)"
              >审核</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup>
import { downLoadBlob } from "@/utils/ruoyi";
import {
  getReportList,
  delReportById,
  updateStatus,
} from "@/api/production/printReport.js";
import { getCoverList } from "@/api/production/coverReport.js";
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();
const route = useRoute();
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `报工单编码`, visible: true },
  { key: 2, label: `报工日期`, visible: true },
  { key: 3, label: `上报人人`, visible: true },
  { key: 4, label: `机台`, visible: true },
  { key: 5, label: `机长`, visible: true },
  { key: 6, label: `当班日期`, visible: true },
  { key: 7, label: `备注`, visible: true },
  { key: 8, label: `创建人`, visible: true },
  { key: 9, label: `创建时间`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});
const { queryParams } = toRefs(data);
const { ems_production_audit, report_status } = proxy.useDict(
  "ems_production_audit",
  "report_status"
);

const getList = () => {
  loading.value = true;

  if (route.query?.type == 4) {
    // 覆膜
    getCoverList({ ...queryParams.value, machineId: userStore.workbranchId })
      .then((res) => {
        loading.value = false;
        res.rows.forEach((item) => {
          item.code = item.coverNo;
          item.reportDate = item.orderDate;
        });
        dataList.value = res.rows;
        total.value = res.total;
      })
      .catch((err) => {
        loading.value = false;
      });
  } else {
    if (route.query?.type) {
      queryParams.value.type = route.query.type;
    }
    getReportList({ ...queryParams.value, machineId: userStore.workbranchId })
      .then((res) => {
        loading.value = false;
        dataList.value = res.rows;
        total.value = res.total;
      })
      .catch((err) => {
        loading.value = false;
      });
  }
};

// 新增
const handleAdd = () => {
  router.push("/production/printReport/printReportDetails");
};
// 编辑
const handleUpdate = (row) => {
  router.push({
    path: "/production/printReport/printReportDetails",
    query: {
      uid: row.id,
    },
  });
};
// 查看
const handleView = (row) => {
  if (row.machineType) {
    if (row.machineType == 2) {
      // 印刷
      router.push({
        path: "/printReportApp",
        query: {
          uid: row.id,
          info: true,
        },
      });
    } else if (row.machineType == 4) {
      // 折页
      router.push({
        path: "/foldingReport",
        query: {
          uid: row.id,
          info: true,
        },
      });
    } else if (row.machineType == 5 || row.machineType == 6) {
      // 裁切
      router.push({
        path: "/cuttingReportApp",
        query: {
          uid: row.id,
          info: true,
        },
      });
    } else if(row.machineType == 7){
        router.push({
        path: "/coverReportApp",
        query: {
          uid: row.id,
          info: true,
        },
      });
    }
  }
  //  跳转
  // router.push({
  //     path: '',
  //     query: {
  //         uid: row.id,
  //         info: true,
  //     },
  // })
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除"' + row.code + '"的报工单？')
    .then(function () {
      return delReportById({ id: row.id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
// 审核
const handleSubmit = (row) => {
  proxy.$modal
    .confirm('是否确认审核"' + row.code + '"的报工单？')
    .then(function () {
      return updateStatus({ status: 1, id: row.id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("审核成功");
    })
    .catch(() => {});
};
// 导出
const handleExport = () => {
  exportPlatemakingList(queryParams.value).then((res) => {
    downLoadBlob(res, "报工单列表");
  });
};
getList();
</script>

<style lang="scss" scoped></style>
